﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KingRoad.EPM.Web.Areas.CompletionMaterials.ViewModels;
using KingRoad.EPM.CompletionMaterialsEntity.Model;
using KingRoad.EPM.Web.Areas.CompletionMaterials.CompletionMaterialsFramework.Common;
using System.Dynamic;

namespace KingRoad.EPM.Web.Areas.CompletionMaterials.Controllers
{
    /// <summary>
    /// 工程项目竣工资料类型管理
    /// </summary>
    public class ProFileTypeController : BaseController
    {

        public ActionResult Index(string type)
        {
            ViewBag.Type = type;
            return View();
        }

        /// <summary>
        /// 工程项目竣工资料类型 Json信息
        /// </summary>
        /// <param name="type">页面类型</param>
        /// <param name="id">项目材料类型id</param>
        /// <returns></returns>
        public JsonResult ProFileTypeJson(string type, string id)
        {
            List<treeNodes> treenodes = new List<treeNodes>();
            if (id == null)
            {
                string sql = " Pft_PID is null and P_ID=" + PID + " and ";
                switch (type)
                {
                    case "1":
                        sql += " Fd_UType='项目法人' ";
                        break;
                    case "2":
                        sql += " Fd_UType='设计单位' ";
                        break;
                    case "3":
                        sql += " Fd_UType='施工单位' and Pft_Section in (" + Get_JL_JC_ZDJL_SG_SegmentCodes() + ") ";
                        break;
                    case "4":
                        sql += " Fd_UType='监理单位' and Pft_Section in (" + Get_JL_JC_ZDJL_SG_SegmentCodes() + ") ";
                        break;
                    case "5":
                        sql += " Fd_UType='相关单位' ";
                        break;
                    default:
                        sql += " 1=1 ";
                        break;
                }
                List<BcdProfiletypeInfo> pList = BcdProfiletypeUIRule.GetEntityListBySql(sql);
                foreach (BcdProfiletypeInfo pInfo in pList)
                {
                    treeNodes pnode = new treeNodes();
                    pnode.pId = "null";
                    pnode.id = pInfo.Pft_ID;
                    pnode.open = "true";
                    pnode.name = GetNodeName(pInfo.Pft_ID);
                    pnode.fdId=pInfo.Fd_ID;
                    treenodes.Add(pnode);

                    List<BcdProfiletypeInfo> pfList = BcdProfiletypeUIRule.GetEntityListBySql(" Pft_PID='" + pInfo.Pft_ID + "' ");
                    foreach (BcdProfiletypeInfo info in pfList)
                    {
                        treeNodes node = new treeNodes();
                        node.pId = info.Pft_PID;
                        node.id = info.Pft_ID;
                        node.name = GetNodeName(info.Pft_ID);
                        node.fdId=info.Fd_ID;
                        int count = BcdProfiletypeUIRule.GetSonCountByPftID(info.Pft_ID);
                        if (count > 0)
                            node.isParent = true;
                        treenodes.Add(node);
                    }
                }
            }
            else
            {
                List<BcdProfiletypeInfo> pfList = BcdProfiletypeUIRule.GetEntityListBySql(" Pft_PID='" + id + "' ");
                foreach (BcdProfiletypeInfo info in pfList)
                {
                    treeNodes node = new treeNodes();
                    node.pId = info.Pft_PID;
                    node.id = info.Pft_ID;
                    node.name = GetNodeName(info.Pft_ID);
                    node.fdId = info.Fd_ID;
                    int count = BcdProfiletypeUIRule.GetSonCountByPftID(info.Pft_ID);
                    if (count > 0)
                        node.isParent = true;
                    treenodes.Add(node);
                }
            }
            return Json(treenodes, JsonRequestBehavior.AllowGet);
        }

        #region 添加根类型
        /// <summary>
        /// 工程项目竣工资料类型 根类型添加页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Create(string type)
        {
            ProFileTypeViewModel vm = new ProFileTypeViewModel();
            vm.TheBcdProfiletypeInfo = new BcdProfiletypeInfo();
            vm.TheBcdProfiletypeInfo.Pft_ID = Guid.NewGuid().ToString().ToUpper();
            vm.TheBcdProfiletypeInfo.P_ID = PRJID;
            vm.TheBcdProfiletypeInfo.P_Name = PRJName;
            vm.TheBcdProfiletypeInfo.P_Code = PRJCode;
            switch (type)
            {
                case "1":
                    vm.TheBcdProfiletypeInfo.Fd_UType = "项目法人";
                    vm.TheBcdProfiletypeInfo.Pft_Unit = PRJUName;
                    vm.TheBcdProfiletypeInfo.Fd_ID = "966E88EB-317A-42FC-91CA-E8D63A000C9D";
                    break;
                case "2":
                    vm.TheBcdProfiletypeInfo.Fd_UType = "设计单位";
                    vm.TheBcdProfiletypeInfo.Pft_Unit = "河南省交通规划设计研究院股份有限公司";
                    vm.TheBcdProfiletypeInfo.Fd_ID = "B900A8E3-3608-44DD-9516-D6C6D97C3F2B";
                    break;
                case "3":
                    vm.TheBcdProfiletypeInfo.Fd_UType = "施工单位";
                    vm.TheBcdProfiletypeInfo.Fd_ID = "5AB7A838-3723-47C3-8E47-E8D077F609FE";
                    break;
                case "4":
                    vm.TheBcdProfiletypeInfo.Fd_UType = "监理单位";
                    vm.TheBcdProfiletypeInfo.Fd_ID = "6E716709-A6F5-47F4-8A28-56B70006F0E3";
                    break;
                case "5":
                    vm.TheBcdProfiletypeInfo.Fd_UType = "相关单位";
                    vm.TheBcdProfiletypeInfo.Fd_ID = "F026510A-48AF-4EF3-B26C-F9A5D0CB9EE3";
                    break;
                default:
                    break;
            }

            BcdFiledataInfo tinfo = BcdFiledataUIRule.GetEntityById(new Guid(vm.TheBcdProfiletypeInfo.Fd_ID));
            vm.TheBcdProfiletypeInfo.Fd_Name = tinfo.Fd_Name;
            vm.TheBcdProfiletypeInfo.Fd_Order = tinfo.Fd_Order;
            vm.TheBcdProfiletypeInfo.Fd_Numb = tinfo.Fd_Numb;
            vm.TheBcdProfiletypeInfo.Fd_Code = tinfo.Fd_Code.Replace("项目", PRJCode);
            vm.TheBcdProfiletypeInfo.Fd_Kit = tinfo.Fd_Kit;
            vm.TheBcdProfiletypeInfo.Fd_Kept = tinfo.Fd_Kept;
            vm.TheBcdProfiletypeInfo.Fd_Table = tinfo.Fd_Table;
            vm.TheBcdProfiletypeInfo.Fd_Remark = tinfo.Fd_Remark;
            //vm.TheBcdProfiletypeInfo.Fd_Important = tinfo.Fd_Important;


            List<BcdFiledataInfo> typeList = BcdFiledataUIRule.GetEntityListBySql(" Fd_PID is null and Fd_UType='" + vm.TheBcdProfiletypeInfo.Fd_UType + "'");
            List<SelectListItem> ftypelist = new List<SelectListItem>();
            foreach (BcdFiledataInfo tInfo in typeList)
            {
                ftypelist.Add(new SelectListItem { Text = tInfo.Fd_Name, Value = tInfo.Fd_ID });
            }
            ViewBag.ftypelist = ftypelist;

            List<SelectListItem> keptlist = new List<SelectListItem>();
            keptlist.Add(new SelectListItem { Text = "永久", Value = "永久" });
            keptlist.Add(new SelectListItem { Text = "长期", Value = "长期" });
            keptlist.Add(new SelectListItem { Text = "短期", Value = "短期" });
            keptlist.Add(new SelectListItem { Text = "30年", Value = "30年" });
            ViewBag.keptlist = keptlist;

            List<SelectListItem> implist = new List<SelectListItem>();
            implist.Add(new SelectListItem { Text = "否", Value = "false" });
            implist.Add(new SelectListItem { Text = "是", Value = "true" });
            ViewBag.implist = implist;

            return View(vm);
        }

        /// <summary>
        /// 工程项目竣工资料类型 根类型添加页面 提交
        /// </summary>
        /// <param name="vm"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult CreatePost(ProFileTypeViewModel vm)
        {
            string name = "";
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                vm.TheBcdProfiletypeInfo.Pft_OperateUserID = GetLoginUserId;              //操作人ID及名称 添加权限时赋值
                vm.TheBcdProfiletypeInfo.Pft_OperateUserName = GetLoginUserRealName;
                vm.TheBcdProfiletypeInfo.Pft_OperateTime = DateTime.Now;
                BcdProfiletypeUIRule.InsertEntity(vm.TheBcdProfiletypeInfo);
                name = (vm.TheBcdProfiletypeInfo.Pft_PID == null ? vm.TheBcdProfiletypeInfo.Pft_Section : vm.TheBcdProfiletypeInfo.Fd_Numb) + " " + vm.TheBcdProfiletypeInfo.Fd_Name;
            });

            dynamic jsonReturn = new ExpandoObject();

            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "添加成功",
                    type = "add",
                    id = vm.TheBcdProfiletypeInfo.Pft_ID,
                    pId = vm.TheBcdProfiletypeInfo.Pft_PID,
                    name = name,
                    navTabId = "CompletionMaterials.ProFileType.Index",
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "添加失败"
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }
        #endregion

        #region 添加自定义类型
        /// <summary>
        /// 工程项目竣工资料类型 自定义类型添加页面
        /// </summary>
        /// <returns></returns>
        public ActionResult CustomCreat(string id)
        {
            ProFileTypeViewModel vm = new ProFileTypeViewModel();
            vm.TheBcdProfiletypeInfo = new BcdProfiletypeInfo();
            vm.TheBcdProfiletypeInfo.Pft_ID = Guid.NewGuid().ToString().ToUpper();
            BcdProfiletypeInfo pinfo = BcdProfiletypeUIRule.GetEntityById(new Guid(id));
            vm.TheBcdProfiletypeInfo.Pft_PID = id;
            vm.TheBcdProfiletypeInfo.Pft_PIds = pinfo.Pft_PIds + ",'" + id + "'";
            vm.TheBcdProfiletypeInfo.P_ID = pinfo.P_ID;
            vm.TheBcdProfiletypeInfo.P_Name = pinfo.P_Name;
            vm.TheBcdProfiletypeInfo.P_Code = pinfo.P_Code;
            vm.TheBcdProfiletypeInfo.Fd_UType = pinfo.Fd_UType;
            vm.TheBcdProfiletypeInfo.Pft_Unit = pinfo.Pft_Unit;
            vm.TheBcdProfiletypeInfo.Fd_Code = pinfo.Fd_Code;
            vm.TheBcdProfiletypeInfo.Fd_Kit = pinfo.Fd_Kit;
            vm.TheBcdProfiletypeInfo.Fd_Kept = pinfo.Fd_Kept;
            vm.TheBcdProfiletypeInfo.Fd_Important = pinfo.Fd_Important;
            vm.TheBcdProfiletypeInfo.Fd_Duty = pinfo.Fd_Duty;

            List<SelectListItem> keptlist = new List<SelectListItem>();
            keptlist.Add(new SelectListItem { Text = "永久", Value = "永久" });
            keptlist.Add(new SelectListItem { Text = "长期", Value = "长期" });
            keptlist.Add(new SelectListItem { Text = "短期", Value = "短期" });
            keptlist.Add(new SelectListItem { Text = "30年", Value = "30年" });
            ViewBag.keptlist = keptlist;

            List<SelectListItem> implist = new List<SelectListItem>();
            implist.Add(new SelectListItem { Text = "否", Value = "false" });
            implist.Add(new SelectListItem { Text = "是", Value = "true" });
            ViewBag.implist = implist;

            return View(vm);
        }

        /// <summary>
        /// 工程项目竣工资料类型 自定义类型添加页面 提交
        /// </summary>
        /// <param name="vm"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult CustomCreatPost(ProFileTypeViewModel vm)
        {
            string name = "";
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                vm.TheBcdProfiletypeInfo.Pft_OperateUserID = GetLoginUserId;              //操作人ID及名称 添加权限时赋值
                vm.TheBcdProfiletypeInfo.Pft_OperateUserName = GetLoginUserRealName;
                vm.TheBcdProfiletypeInfo.Pft_OperateTime = DateTime.Now;
                BcdProfiletypeUIRule.InsertEntity(vm.TheBcdProfiletypeInfo);
                name = (vm.TheBcdProfiletypeInfo.Pft_PID == null ? vm.TheBcdProfiletypeInfo.Pft_Section : vm.TheBcdProfiletypeInfo.Fd_Numb) + " " + vm.TheBcdProfiletypeInfo.Fd_Name;
            });

            dynamic jsonReturn = new ExpandoObject();

            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "添加成功",
                    type = "add",
                    id = vm.TheBcdProfiletypeInfo.Pft_ID,
                    pId = vm.TheBcdProfiletypeInfo.Pft_PID,
                    name = GetNodeName(vm.TheBcdProfiletypeInfo.Pft_ID),
                    navTabId = "CompletionMaterials.ProFileType.Index",
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "添加失败"
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }
        #endregion

        #region 修改类型
        /// <summary>
        /// 工程项目竣工资料类型 修改页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Edit(string id)
        {
            ProFileTypeViewModel vm = new ProFileTypeViewModel();
            vm.TheBcdProfiletypeInfo = BcdProfiletypeUIRule.GetEntityById(new Guid(id));

            List<SelectListItem> keptlist = new List<SelectListItem>();
            keptlist.Add(new SelectListItem { Text = "永久", Value = "永久" });
            keptlist.Add(new SelectListItem { Text = "长期", Value = "长期" });
            keptlist.Add(new SelectListItem { Text = "短期", Value = "短期" });
            keptlist.Add(new SelectListItem { Text = "30年", Value = "30年" });
            ViewBag.keptlist = keptlist;

            List<SelectListItem> implist = new List<SelectListItem>();
            implist.Add(new SelectListItem { Text = "否", Value = "false" });
            implist.Add(new SelectListItem { Text = "是", Value = "true" });
            ViewBag.implist = implist;

            return View(vm);
        }

        /// <summary>
        /// 工程项目竣工资料类型 修改页面 提交
        /// </summary>
        /// <param name="vm"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult EditPost(ProFileTypeViewModel vm)
        {
            string name = "";
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                vm.TheBcdProfiletypeInfo.Pft_OperateUserID = GetLoginUserId;              //操作人ID及名称 添加权限时赋值
                vm.TheBcdProfiletypeInfo.Pft_OperateUserName = GetLoginUserRealName;
                vm.TheBcdProfiletypeInfo.Pft_OperateTime = DateTime.Now;
                BcdProfiletypeUIRule.UpdateEntity(vm.TheBcdProfiletypeInfo);
                name = (vm.TheBcdProfiletypeInfo.Pft_PID == null ? vm.TheBcdProfiletypeInfo.Pft_Section : vm.TheBcdProfiletypeInfo.Fd_Numb) + " " + vm.TheBcdProfiletypeInfo.Fd_Name;
            });

            dynamic jsonReturn = new ExpandoObject();

            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "修改成功",
                    type = "edit",
                    id = vm.TheBcdProfiletypeInfo.Pft_ID,
                    pId = vm.TheBcdProfiletypeInfo.Pft_PID,
                    name = GetNodeName(vm.TheBcdProfiletypeInfo.Pft_ID),
                    navTabId = "CompletionMaterials.ProFileType.Index",
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "修改失败"
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }
        #endregion

        #region 归档材料类型选择
        /// <summary>
        /// 归档材料类型选择弹出框
        /// </summary>
        /// <param name="id">项目材料类型id</param>
        /// <returns></returns>
        public ActionResult Select(bool bridge,string id)
        {
            Guid tempId = Guid.NewGuid();
            ViewBag.tempId = tempId.ToString();
            ViewBag.pftId = id;
            ViewBag.bridge = bridge;
            BcdProfiletypeInfo pinfo = BcdProfiletypeUIRule.GetEntityById(new Guid(id));
            if (bridge)
            {
                //添加桥梁信息
                ViewBag.id = "C1F9B4D4-9BF4-43D4-9AC9-CE3976F5A1AF";
                List<BcdProfiletypeInfo> list = BcdProfiletypeUIRule.GetEntityListBySql(" P_ID=" + pinfo.P_ID + " and Pft_Section='"+pinfo.Pft_Section+"' and Fd_ID='C1F9B4D4-9BF4-43D4-9AC9-CE3976F5A1AF' ");
                ViewBag.code = "HNYLX（YLTJ-1）/5.4."+(list.Count+1);
            }
            else
            {
                ViewBag.id = pinfo.Fd_ID;
                string sql = " Pft_PIds like '%" + id + "%' and Fd_ID is not null ";
                //如果是互通立交工程则排除桥梁信息
                if (pinfo.Fd_ID == "BFADFD31-5048-423E-B8B8-D479D15F7879")
                    sql = " Pft_PIds like '%" + id + "%' and Fd_ID is not null and Fd_ID!='C1F9B4D4-9BF4-43D4-9AC9-CE3976F5A1AF' and Fd_PIds not like '%C1F9B4D4-9BF4-43D4-9AC9-CE3976F5A1AF%' ";
                List<BcdProfiletypeInfo> pList = BcdProfiletypeUIRule.GetEntityListBySql(sql);
                //添加临时表
                foreach (BcdProfiletypeInfo info in pList)
                {
                    BcdProjectfiletempInfo pftInfo = new BcdProjectfiletempInfo();
                    pftInfo.P_ID = tempId.ToString();
                    pftInfo.Fd_ID = info.Fd_ID;
                    pftInfo.P_Fd_State = true;
                    BcdProjectfiletempUIRule.InsertEntity(pftInfo);
                }
            }
            return View();
        }

        /// <summary>
        /// 异步加载归档材料类型Json数据
        /// </summary>
        /// <param name="tempId">临时表id</param>
        /// <param name="pftId">项目材料类型id</param>
        /// <param name="id">类型id</param>
        /// <returns></returns>
        public JsonResult AsyncSelectJson(string tempId, string pftId, string id,bool bridge)
        {
            List<treeNodes> treenodes = new List<treeNodes>();
            string sql = " Fd_PID='" + id + "' ";
            //如果是互通立交工程
            if (id == "BFADFD31-5048-423E-B8B8-D479D15F7879")
                sql = " Fd_PID='" + id + "' and Fd_ID!='C1F9B4D4-9BF4-43D4-9AC9-CE3976F5A1AF' ";
            List<BcdFiledataInfo> fList = BcdFiledataUIRule.GetEntityListBySql(sql);
            foreach (BcdFiledataInfo info in fList)
            {
                treeNodes node = new treeNodes();
                node.id = info.Fd_ID;
                node.pId = info.Fd_PID;
                node.name = info.Fd_Numb + " " + info.Fd_Name;
                //读取 工程项目与材料类型关系表
                List<BcdProfiletypeInfo> pList = BcdProfiletypeUIRule.GetEntityListBySql(" Pft_PIds like '%" + pftId + "%' and Fd_ID='" + info.Fd_ID + "' ");
                if (pList.Count > 0 && !bridge)
                {
                    node.@checked = true;
                }
                //读取 工程项目与材料类型关系表（临时表在修改时用，平时数据为空）
                List<BcdProjectfiletempInfo> pftlist = BcdProjectfiletempUIRule.GetEntityListBySql(" P_ID='" + tempId + "' and  Fd_ID='" + info.Fd_ID + "' ");
                if (pftlist.Count > 0)
                {
                    BcdProjectfiletempInfo pftInfo = pftlist[0];
                    if (pftInfo.P_Fd_State == false)
                        node.@checked = false;
                    else
                        node.@checked = true;
                }
                if (BcdFiledataUIRule.GetCountByFId(info.Fd_ID) > 0)
                    node.isParent = true;
                treenodes.Add(node);
            }
            return Json(treenodes, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 在临时表中 勾选材料类型
        /// </summary>
        /// <param name="tempId">临时表id</param>
        /// <param name="pId">父类型id</param>
        /// <param name="id">类型id</param>
        /// <returns></returns>
        public JsonResult TempSelect(string tempId, string pId, string id)
        {
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                if (id != null && id != "null")
                {
                    BcdProjectfiletempUIRule.TempAdd(tempId, pId, id);
                }
            });

            dynamic jsonReturn = new ExpandoObject();

            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "选择成功",
                    pId = "",
                    id = id,
                    name = "",
                    navTabId = "CompletionMaterials.ProFileType.Index",
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "选择失败",
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }
        /// <summary>
        /// 在临时表中 排除材料类型
        /// </summary>
        /// <param name="tempId">临时表id</param>
        /// <param name="id">类型id</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult TempDelete(string tempId, string id)
        {
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                BcdProjectfiletempUIRule.TempDelete(tempId, id);
            });

            dynamic jsonReturn = new ExpandoObject();

            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "排除成功",
                    pId = "",
                    id = id,
                    name = "",
                    navTabId = "CompletionMaterials.ProFileType.Index",
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "排除失败",
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }

        /// <summary>
        /// 给项目添加勾选的材料类型
        /// </summary>
        /// <param name="type">添加类型（sel=选择，cancel=取消）</param>
        /// <param name="tempId">临时表id</param>
        /// <param name="pftId">项目材料类型id</param>
        /// <param name="id">类型id</param>
        public JsonResult SelectPost(string type, string tempId, string pftId, string id, bool bridge,string name,string code)
        {
            bool isParent = false;
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                if (type == "sel")
                {
                    if (bridge)
                    {
                        //添加桥梁信息
                        BcdProfiletypeInfo pinfo = BcdProfiletypeUIRule.GetEntityById(new Guid(pftId));
                        BcdProfiletypeInfo info = new BcdProfiletypeInfo();
                        info.Pft_ID = Guid.NewGuid().ToString().ToUpper();
                        info.Pft_PID = pftId;
                        info.Pft_PIds = pinfo.Pft_PIds + ",'" + pinfo.Pft_ID + "'";
                        info.P_ID = pinfo.P_ID;
                        info.P_Name = pinfo.P_Name;
                        info.P_Code = pinfo.P_Code;
                        info.Pft_Section = pinfo.Pft_Section;
                        info.Pft_Unit = pinfo.Pft_Unit;
                        info.Pft_OperateUserID = GetLoginUserId;
                        info.Pft_OperateUserName = GetLoginUserRealName;
                        info.Pft_OperateTime = DateTime.Now;
                        info.Fd_Duty = pinfo.Fd_Duty;
                        BcdFiledataInfo tinfo = BcdFiledataUIRule.GetEntityById(new Guid("C1F9B4D4-9BF4-43D4-9AC9-CE3976F5A1AF"));
                        info.Fd_UType = tinfo.Fd_UType;
                        info.Fd_ID = tinfo.Fd_ID;
                        info.Fd_Name = name;
                        info.Fd_Order = tinfo.Fd_Order;
                        info.Fd_Numb = tinfo.Fd_Numb;
                        info.Fd_Code = code; ;
                        info.Fd_Kit = tinfo.Fd_Kit;
                        info.Fd_Kept = tinfo.Fd_Kept;
                        info.Fd_Table = tinfo.Fd_Table;
                        info.Fd_Remark = tinfo.Fd_Remark;
                        //info.Fd_Important=tinfo.Fd_Important;
                        BcdProfiletypeUIRule.InsertEntity(info);
                        //添加子类型信息
                        BcdProjectfiletempUIRule.SaveSel(tempId, info.Pft_ID, id, GetLoginUserId, GetLoginUserRealName);
                    }
                    else
                    {
                        //添加子类型信息
                        BcdProjectfiletempUIRule.SaveSel(tempId, pftId, id, GetLoginUserId, GetLoginUserRealName);
                    }
                    if (BcdProfiletypeUIRule.GetSonCountByPftID(pftId) > 0)
                        isParent = true;
                }
                if (type == "cancel")
                    BcdProjectfiletempUIRule.DeleteByPID(tempId);
            });
            dynamic jsonReturn = new ExpandoObject();
            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "",
                    type = type,
                    isParent = isParent,
                    pId = "",
                    id = pftId,
                    name = "",
                    navTabId = "CompletionMaterials.ProFileType.Index",
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "操作失败",
                };
            }
            return new JsonResult()
            {
                Data = jsonReturn
            };
        }

        #endregion

        /// <summary>
        /// 标段信息弹出框
        /// </summary>
        /// <returns></returns>
        public ActionResult SectionUnit()
        {
            return View();
        }
        /// <summary>
        /// 获取材料类型Json信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult FileTypeChang(string id)
        {
            BcdFiledataInfo info = new BcdFiledataInfo();
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                info = BcdFiledataUIRule.GetEntityById(new Guid(id));
                info.Fd_Code = info.Fd_Code.Replace("项目", PRJCode);
            });
            dynamic jsonReturn = new ExpandoObject();
            if (isScuess)
            {
                jsonReturn = new
                {
                    Fd_Name = info.Fd_Name,
                    Fd_Order = info.Fd_Order,
                    Fd_Numb = info.Fd_Numb,
                    Fd_Code = info.Fd_Code.Replace("项目", PRJCode),
                    Fd_Kit = info.Fd_Kit,
                    Fd_Kept = info.Fd_Kept,
                    Fd_Table = info.Fd_Table,
                    Fd_Remark = info.Fd_Remark,
                    message = "获取材料类型信息成功！"
                };
            }
            else
            {
                jsonReturn = new
                {
                    message = "获取材料类型信息失败！"
                };
            }
            return new JsonResult()
            {
                Data = jsonReturn
            };
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">类型id</param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Delete(string id)
        {
            var isScuess = TryCatchCommand.TryExec(() =>
            {
                if (id != null && id != "null")
                    BcdProfiletypeUIRule.DeleteEntity(id);
            });

            dynamic jsonReturn = new ExpandoObject();

            //保存成功
            if (isScuess)
            {
                jsonReturn = new
                {
                    statusCode = "200",
                    message = "删除成功",
                    type = "delete",
                    id = id,
                    navTabId = "CompletionMaterials.ProFileType.Index",
                    rel = "",
                    callbackType = "",
                    forwardUrl = "",
                    confirmMsg = ""
                };
            }
            else
            {
                jsonReturn = new
                {
                    statusCode = "300",
                    message = "删除失败",
                };
            }

            return new JsonResult()
            {
                Data = jsonReturn
            };
        }

        /// <summary>
        /// 工程项目竣工资料类型  查看
        /// </summary>
        /// <param name="tab_i">要显示的tab标签序号</param>
        /// <param name="type">页面类型</param>
        /// <param name="id">材料类型</param>
        /// <returns></returns>
        public ActionResult Detail(string tab_i, string type, string id)
        {
            ProFileTypeViewModel vm = new ProFileTypeViewModel();
            ViewBag.Tab_i = tab_i;
            ViewBag.Type = type;
            ViewBag.Id = id;
            if (id != null && id != "null" && id != "")
            {
                //工程项目竣工资料类型详情
                vm.TheBcdProfiletypeInfo = BcdProfiletypeUIRule.GetEntityById(new Guid(id));
            }
            else
            {
                vm.TheBcdProfiletypeInfo = new BcdProfiletypeInfo();
            }
            return View(vm);
        }
    }
}
